﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZX.HIS.Model;
using ZX.HIS.BLL;
using ZX.HIS.Common;
using ZX.HIS.SQLServerDAL;

namespace ZX.HIS.UI.Controllers
{
    public class AdminController : Controller
    {
        //
        // GET: /Admin/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Regist()
        {
            return View();
        }


        public int FnLogin()
        {
            ZX.HIS.BLL.Hospital bll = new BLL.Hospital();


            string UserName = Request["UserName"].ToString();
            string UserPwd = Request["UserPwd"].ToString();

            List<ZX.HIS.Model.UserInfo> list = bll.loginHospital(UserName, UserPwd);

            ZX.HIS.Model.UserInfo model = list.Where(p => p.UserName == UserName && p.UserPwd == UserPwd).FirstOrDefault();

            Session["UserInfo"] = model; //人员管理信息
            if (model != null)
            {
                HttpCookie myCookie = new HttpCookie("MyCookie");//初使化并设置Cookie的名称
                DateTime dt = DateTime.Now;
                TimeSpan ts = new TimeSpan(3, 0, 0, 0, 0);//过期时间为3天
                myCookie.Expires = dt.Add(ts);//设置过期时间
                myCookie.Values.Add("UserName", model.UserName);
                myCookie.Values.Add("UserId", model.UserId.ToString());
                myCookie.Values.Add("UserPwd", model.UserPwd);
                myCookie.Values.Add("UserChineseName", HttpUtility.UrlEncode(model.UserChineseName));
                if (model.UserSex != null)
                {
                    myCookie.Values.Add("UserSex", model.UserSex.ToString());
                }
                if (model.UserSex != null)
                {
                    myCookie.Values.Add("UserPhone", HttpUtility.UrlEncode(model.UserPhone.ToString()));
                }
                if (model.UserEmail != null)
                {
                    myCookie.Values.Add("UserEmail", HttpUtility.UrlEncode(model.UserEmail.ToString()));
                }
                if (model.DepartId != null)
                {
                    myCookie.Values.Add("DepartId", model.DepartId.ToString());
                }
                if (model.UserSecrecy != null)
                {
                    myCookie.Values.Add("UserSecrecy", model.UserSecrecy.ToString());
                }
                if (model.HospitalId != null)
                {
                    myCookie.Values.Add("HospitalId", model.HospitalId.ToString());
                }
                if (model.UserState != null)
                {
                    myCookie.Values.Add("UserState", model.UserState.ToString());
                }
                Response.AppendCookie(myCookie);
                if (model.UserState != 1)
                {
                    return 2;
                }
                return 1;
            }
            else
            {
                return 0;
            }
        }

        public int FnRegist()
        {
            ZX.HIS.BLL.Hospital bll = new ZX.HIS.BLL.Hospital();

            string HospitalUserName = Request["HospitalUserName"].ToString();

            string HospitalPhone = Request["HospitalPhone"].ToString();

            string HospitalName = Request["HospitalName"].ToString();

            string HospitalAddre = Request["HospitalAddre"].ToString();

            string HospitalNo = Request["HospitalNo"].ToString();

            string UserName = Request["UserName"].ToString();
            string UserPwd = Request["UserPwd"].ToString();

            ZX.HIS.Model.Hospital model = new Model.Hospital();
            model.HospitalUserName = HospitalUserName;
            model.HospitalUserName = HospitalUserName;
            model.HospitalNo = HospitalNo;
            model.HospitalName = HospitalName;
            model.HospitalAddre = HospitalAddre;
            model.HospitalPhone = HospitalPhone;

            int i = bll.registHospital(model);
            if (i > 0)
            {

                return bll.addUserInfo(i, UserName, UserPwd, HospitalUserName);
            }
            else
            {
                return 0;
            }
        }
    }
}
